<?php

namespace Portal\Domain;

use Portal\Model\RoleMenu as RoleMenuModel;

class RoleMenu
{
    public function getMenuIds($roleIds)
    {
        $returnData = [];
        $menuIdDatas = RoleMenuModel::notorm()->select('menu_id')->where(['role_id' => $roleIds])->fetchAll();
        if (is_array($menuIdDatas) && !empty($menuIdDatas)) {
            $menuIds = [];
            foreach ($menuIdDatas as $menuIdData) {
                $menuIds[] = $menuIdData['menu_id'];
            }
            $returnData = $menuIds;
        }
        // \PhalApi\DI()->cache->set($cacheKey, $returnData);
        return $returnData;
    }

    public function getRoleMenuIds($roleId)
    {
        //判断是否root
        if ($roleId == 1) {
            $isRoot = true;
        } else {
            $isRoot = false;
        }
        $returnData = [];
        $notorm = RoleMenuModel::notorm()->select('menu_id');
        if ($isRoot == false) {
            $roleIds = [];
            $roleIds[] = $roleId;
            $notorm->where(['role_id' => $roleIds]);
        }
        //超级管理员查询所有
        $menuIdDatas = $notorm->fetchAll();
        if (is_array($menuIdDatas) && !empty($menuIdDatas)) {
            $menuIds = [];
            foreach ($menuIdDatas as $menuIdData) {
                $menuIds[] = $menuIdData['menu_id'];
            }
            //去重
            $menuIds = array_unique($menuIds);
            //查询menu数据
            foreach ($menuIds as $menuId) {
                $returnData[] = strval($menuId);
            }
        }
        // \PhalApi\DI()->cache->set($cacheKey, $returnData);
        return $returnData;
    }
    //修改权限对应菜单权限
    public function updateRoleMenus($roleId, $newMenuIds)
    {

        # 使用事务确保数据一致性
        $oldMenuIds = [];
        $adminRoleModel = new RoleMenuModel();
        //开启事物
        $adminRoleModel::notorm()->transaction('BEGIN');
        $oldMenuDatas = $adminRoleModel->notorm()->select('menu_id')->where(['role_id' => $roleId])->fetchAll();
        foreach ($oldMenuDatas as $oldMenuData) {
            $oldMenuIds[] = $oldMenuData['menu_id'];
        }
        //获取新增加的菜单id
        $addMenuIds = array_diff($newMenuIds, $oldMenuIds);
        //获取删除掉的菜单id
        $delMenuIds = array_diff($oldMenuIds, $newMenuIds);
        //删除处理
        if (!empty($delMenuIds)) {
            $updateStatus = $adminRoleModel::notorm()->where(['role_id' => $roleId,'menu_id' => $delMenuIds])->delete();
        }

        //新增处理
        if (!empty($addMenuIds)) {
            $insertDatas = [];
            foreach ($addMenuIds as $addMenuId) {
                $insertData['menu_id'] = $addMenuId;
                $insertData['role_id'] = $roleId;
                $insertDatas[] = $insertData;
            }
            $updateStatus = $adminRoleModel->insertMore($insertDatas);
        }
        # 使用事务确保数据一致性
        if ($updateStatus) {
            $adminRoleModel->notorm()->transaction('COMMIT');
        } else {
            $adminRoleModel->notorm()->transaction('ROLLBACK');

        }
        //更新用户信息缓存
        $adminDomain = new \Portal\Domain\Admin();
        $adminDomain->flushUserInfoCache();
    }


}
